package br.lsi.supercurriculum.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import br.lsi.supercurriculum.entidades.Contato;
import br.lsi.supercurriculum.entidades.Endereco;
import br.lsi.supercurriculum.entidades.Pessoa;

/**
 * @author Dannylo Johnathan
 * @since 21/08/2012
 */

/**
 * Classe responsável pela conexão com o Banco de Dados.
 */

@SuppressWarnings("deprecation")
public class HibernateUtil {
	private static SessionFactory sessionFactory;

    private HibernateUtil() {
    }

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                AnnotationConfiguration ac = new AnnotationConfiguration();
                ac.addAnnotatedClass(Pessoa.class);
                ac.addAnnotatedClass(Endereco.class);
                ac.addAnnotatedClass(Contato.class);

                sessionFactory = ac.configure().buildSessionFactory();

                SchemaExport se = new SchemaExport(ac);
                se.create(true,true);
                
            } catch (Throwable ex) {
                System.err.println("Inicialização Falha" + ex);

                throw new ExceptionInInitializerError(ex);
            }
            return sessionFactory;
        } else {
            return sessionFactory;
        }

    }

    public static void main(String[] a){
        HibernateUtil.getSessionFactory();
    }

}
